package com.jinsi.service.impl;

import com.jinsi.mapper.FolderMapper;
import com.jinsi.pojo.Folder;
import com.jinsi.pojo.TreeNode;
import com.jinsi.service.FolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FolderServiceImpl implements FolderService {

    @Autowired
    private FolderMapper folderMapper;

    @Override
    public void addFolder(Folder folder) throws Exception{

        int num = folderMapper.insertFolder(folder);
        if(num == 0){
            throw  new RuntimeException();
        }
    }

    @Override
    public List<TreeNode> getTreeData(Integer equpid,String equipname) {

        List<TreeNode> sublist = new ArrayList<>();
        List<Folder> fs = folderMapper.getFolderByEquipId(equpid);

        for(Folder f:fs){
            System.out.println(f);

            TreeNode subtreeNode = new TreeNode();
            subtreeNode.setId(f.getId());
            subtreeNode.setTitle(f.getName());

            sublist.add(subtreeNode);
        }
        ////////////////

        List<TreeNode> list = new ArrayList<>();

        TreeNode treeNode = new TreeNode();
        treeNode.setId(equpid);
        treeNode.setTitle(equipname);
        treeNode.setChildren(sublist);
        list.add(treeNode);

        return list;
    }
}
